<?php

require_once './core/p_pre.php';
process($db, $userId, $deviceId);
require_once './core/p_post.php';

function process(&$db, $userId, $deviceId) {
	require_once './core/dataset.php';
	$keyPage;
	if ( param('v') == 'm' ) {
		$keyPage = 'key_m.php';
	} else {
		$keyPage = 'key_d.php';
	}
	$kingdom = param('kingdom');
	$continent = param('continent');
	
	$sqlCnt;
	$sql;
	$where = "";
	
	$searchTerm = param('searchTerm');
	if ( strlen($searchTerm) > 0 ) {
		$searchTerm = sqlEscape($searchTerm);
	}
	$sqlCnt = "SELECT count(*) cnt FROM key_data";
	$sql = "SELECT * FROM key_data ";

	$where = CROUtil::join($where, "Length (dataDir) > 0", 'AND');
	if ( strlen($kingdom) > 0 ) {
		$where = CROUtil::join($where, "kingdom = '$kingdom'", 'AND');
	}
	if ( strlen($continent) > 0 ) {
		$where = CROUtil::join($where, "continent = '$continent'", 'AND');
	}
	
	if (strlen($searchTerm) > 0) {
		$where = CROUtil::join($where, "label like '$searchTerm%'", 'AND');
	}
	
	if ( $where ) {
		$sql .= " WHERE $where";
		$sqlCnt .= " WHERE $where";
	}
		
	$totalCnt = $db->selectCnt($sqlCnt);
	if ( $totalCnt < 1 ) {
		return;
	}
	
	$orderBy = param('orderBy');
	if ( strlen($orderBy) < 1 ) {
		$orderBy = "label";
	}
	$sql .= " ORDER BY $orderBy";

	$limitCount = param('limitCount');
	if ( !$limitCount ) {
		$limitCount = 10000;
	}
	if ( $limitCount ) {
		$limitStart = param('limitStart');
		if ( !$limitStart ) {
			$limitStart = '0';
		}
		$limit = " LIMIT $limitStart, $limitCount";
	}
	$sql .= " $limit";
	
//	echo $sql;
	
	$result =& $db->doQuery($sql);
	$list = array();
	$cnt = 0;
	if ( $result ) {		
		while($row = mysql_fetch_array($result)){
			$r = new DataSet();
			$r->id = $row["id"];
			$r->inkURL = checkGet($row, 'inkUrl');
			$r->label = checkGet($row, 'label');
			$r->title = checkGet($row, 'title');			
			$r->subTitle = checkGet($row, 'subTitle');			
			$r->description = checkGet($row, 'description');
			$r->imagePath = checkGet($row, 'imagePath');
			$r->dataDir = checkGet($row, 'dataDir');
			$list[$cnt] = $r;
			
			$cnt++;
		}
	}
	
	$len = count($list);
	if ( param('format') == 'json' ) {
		echo '{"total":'.$totalCnt.',"dataList":'.json_encode($list).'}';
		return;
	}
	
	$title;
	if ( strlen($kingdom) > 0 ) {
		$title = $kingdom;
	} else if ( strlen($continent) > 0 ) {
		$title = $continent;
	} else {
		$title = 'Data Sets';
	}
	

?>

<?php
for ($i=0; $i<$len; $i++) {
	$d = $list[$i];
?>
{{Metadata ID Tool Tabulation 
| Type                 = IdentificationTool
| Title                = <?php echo "$d->label\n"; ?>
| Description          = <?php echo "$d->title\n"; ?>
| Subject Category     = 
| Host Application     = Web Browser, Mobile
| ID Tool Structure    = Multi-access
| Language             = en
| License Statement    = 
| Interactivity        = Dynamic
| Best Quality URI     = http://www.pathkey.org/key_d.php?datasetId=<?php echo "$d->id\n"; ?>
| Best Quality Availability = Online (free)
}}
	
<?php }	?>




<?php } ?>
